/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cnab.local.paulista;

import java.io.File;
import java.util.ArrayList;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 * @author MVCapital Modificado em 14/02/2014
 */
public class ReaderRetornoPaulista 
{
  private ArrayList<FileCNABPaulista> filesCNAB = new ArrayList<FileCNABPaulista>();
  private String rootOriginPath = "C:\\Tmp\\";
  private String rootDestinyPath = "C:\\Tmp\\";

  public static void main(String[] args)  
  {
    File fileRetorno = new File("C:\\Tmp\\CNAB\\CB010200.RET");
    ArrayList<FileCNABPaulista> listFiles = new ArrayList<FileCNABPaulista>();
    listFiles.add(new FileCNABPaulista(fileRetorno));
    
    //ReaderRetornoBradesco reader = new ReaderRetornoBradesco(listFiles);
    ReaderRetornoPaulista reader = new ReaderRetornoPaulista("C:\\Tmp\\CNAB");
    
    reader.writeFilesCNAB();
    
  }
  
  public ReaderRetornoPaulista()
  {
    
  }

  public ReaderRetornoPaulista(String rootOriginPath)
  {
    this.rootOriginPath = rootOriginPath;
    ArrayList<File> listFiles = filesInDirectory(getRootOriginPath());
    for(File file:listFiles)
    {
      this.filesCNAB.add(new FileCNABPaulista(file));
    }
  }
  
  public ReaderRetornoPaulista(ArrayList<FileCNABPaulista> filesCNAB)
  {
    this.setFilesCNAB(filesCNAB);
  }

  public void writeFilesCNAB()
  {
    for(FileCNABPaulista fileCNAB:this.filesCNAB)
    {
      String outputFilename = rootDestinyPath + fileCNAB.getNameCSV();
      FileCNABPaulista.writeCsvFile(outputFilename, fileCNAB.getLotes());
    }
  }
  
  public static ArrayList<File> filesInDirectory(String directory)
  {
    ArrayList<File> filesList = new ArrayList<File>();
    File folder = new File(directory);
    File[] listOfFiles = folder.listFiles();
    
    if (folder.listFiles()==null)
    {
      return null;
    }
    else
    {
      for (int i = 0; i < listOfFiles.length; i++) 
      {
        filesList.add(listOfFiles[i]);
      }
      return filesList;
    }
  }
  public ArrayList<FileCNABPaulista> getFilesCNAB() 
  {
    return filesCNAB;
  }

  public void setFilesCNAB(ArrayList<FileCNABPaulista> filesCNAB) 
  {
    this.filesCNAB = filesCNAB;
  }

  public String getRootOriginPath() 
  {
    return rootOriginPath;
  }

  public void setRootOriginPath(String rootOriginPath) 
  {
    this.rootOriginPath = rootOriginPath;
  }

  public String getRootDestinyPath() 
  {
    return rootDestinyPath;
  }

  public void setRootDestinyPath(String rootDestinyPath) 
  {
    this.rootDestinyPath = rootDestinyPath;
  }

  
}
